import {
    wrapToVdom
} from "./utils";
import {
    REACT_ELEMENT
} from "./constants";

/**
 * 
 * @param {*} type 标签
 * @param {*} config props
 * @param {*} children 子元素
 * @returns 
 */
function createElement(type, config, children) {
    let key, ref
    if (config) {
        key = config.key //引用真实的dom元素
        ref = config.ref //
        delete config.ref
        delete config.key
    }
    let props = {
        ...config
    }
    // 如果arguments的长度大于3，说明元素内部有多个子元素
    if (arguments.length > 3) {
        props.children = Array.prototype.slice.call(arguments, 2).map(wrapToVdom)
    } else if (arguments.length === 3) {
        props.children = wrapToVdom(children);
    }
    return { //这就是vdom
        $$typeof: REACT_ELEMENT, //元素的类型
        type, //标签类型
        ref,
        key,
        props, //classname style  children
    }
}

const React = {
    createElement
}
export default React